<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>树</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="${ctxPath}/static/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="${ctxPath}/static/css/public.css" media="all">
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-inline">
                <input type="text" name="updateDate" id="updateDate" placeholder="请输入" class="layui-input">
            </div>

            <div class="layui-inline">
                <button class="layui-btn layuiadmin-btn-tree" lay-submit lay-filter="LAY-tree-search">
                    <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                </button>
            </div>


        </div>

        <div class="layui-card-body">
            <div class="layui-form layui-form-item">
                <input name="open[yes]" title="展开" lay-filter="open" checked="" type="checkbox">
            </div>
            <ul id="treeElem" class="ztree"></ul>
        </div>
    </div>
</div>

<script src="${ctxPath}/static/js/jquery-3.2.1.min.js"></script>
<script src="${ctxPath}/static/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="${ctxPath}/static/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    var zTreeObj;
    layui.use(["element","form", "zTree"], function () {
        var $ = layui.$
            , form = layui.form
            , zTree = layui.zTree;

        //参数
        var obj = {
            'url': '${url}',
            'type': '${type}',
            'extId': '${extId}',
            'search': '${search}',
            'check': '${check}',
            'edit': '${edit}',
            'rootId': '${rootId}',
            'elem': '${elem}',
            'showElem': '${showElem}',
            'value': '${value}',
            'label': '${label}'
        };
        console.log(obj);
        var setting = {
            async: {
                enable: true,
                url: obj.url,
                // otherParam: ["isAll",${isAll!},"type",${type!},'extId','${extId!}'],
                type: "get"
            },
            data: {
                simpleData: {
                    enable: true,
                    idKey: "id",
                    pIdKey: "pId",
                    rootPId: obj.rootId
                }
            },
            check: {
                enable: obj.check === 'true' ? true : false
            },
            callback: {
                onDblClick: function (event, treeId, treeNode) {
                    // var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                    // $(obj.elem, parent.document).val(treeNode.id);
                    // $(obj.showElem, parent.document).children('input').val(treeNode.name);
                    // parent.layer.close(index);
                },
                onClick: function (event, treeId, treeNode) {
                    var currZtree = zTree.getZTreeObj(treeId);
                    var expandFlag = treeNode.open ? false : true;
                    currZtree.expandNode(treeNode, expandFlag, false, true);
                },
                onAsyncSuccess: function (event, treeId, treeNode) {
                    var currZtree = zTree.getZTreeObj(treeId);
                    var nodeList = currZtree.getNodes();
                    currZtree.expandNode(nodeList[0], true);

                    if (obj.check === 'true') {
                        var pnodes = [];
                        var ids = obj.value.split(',');
                        ids.forEach(function (item, index) {
                            var node = currZtree.getNodeByParam("id", item, null) || '';
                            node.checked = true;
                            currZtree.updateNode(node);
                            var pnode = currZtree.getNodeByParam("id", node.pId, null);
                            pnodes.push(pnode);
                        });
                        if (pnodes[0] != null) {
                            pnodes.forEach(function (node, index) {
                                node.checked = true;
                                currZtree.updateNode(node);
                                currZtree.expandNode(node, true);
                            });
                        }
                    } else {
                        var node = currZtree.getNodeByParam("id", obj.value, null) || '';
                        currZtree.selectNode(node);
                        var pnode = currZtree.getNodeByParam("id", node.pId, null);
                        currZtree.expandNode(pnode, true);
                    }
                }
            }
        };

        zTreeObj = zTree.init($("#treeElem"), setting);

        form.on('checkbox(open)', function (data) {
            zTreeObj.expandAll(data.elem.checked)
        });
    })
</script>
</body>
</html>